package com.city.helper;

import com.city.service.report.AuthService;
import com.city.service.report.UserService;
import com.city.utils.SignatureUtil;
import org.springframework.data.redis.core.StringRedisTemplate;

import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;


/**
 * @deprecation: 权限助手函数
 * @email: leichuqiao@126.com
 * @author: 雷楚桥
 * @date: 2018/7/22 17:50
 */

@Component
public class AuthHelper {

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private AuthService authService;
    @Resource
    private UserService userS;
    @Resource
    private HttpServletRequest httpServletRequest;
    @Resource
    private HttpSession httpSession;

    private static StringRedisTemplate redis ;
    private static AuthService authS;
    private static HttpServletRequest request;
    private static HttpSession session;

    @PostConstruct
    public void init() {
        redis = stringRedisTemplate;
        authS = authService;
        request = httpServletRequest;
        session = httpSession;
    }

    /**
     * @deprecation 创建一个sign,长度为16
     * @param id Integer 用户id
     * @return String
     * @author 雷楚桥
     * @date 2018-08-31 15:08
     */
    public String updateSign(Integer id) {
        String sign;
        Integer len = 16;
        while(true){
            String timeStamp = SignatureUtil.getTimeStamp();
            String nonce = SignatureUtil.getRandomStr(len);
            sign = SignatureUtil.createSignature(timeStamp, nonce);
            if(!redis.hasKey(sign)){
                redis.opsForValue().set(sign, id + "",7, TimeUnit.DAYS);
                break;
            }
        }
        return sign;
    }

    /**
     * @deprecation 获取sign
     * @return String value值
     * @author 雷楚桥
     * @date 2018-08-31 17:54
     */
    public String getSign(){
        String sign = null;
        Cookie cookies[] = request.getCookies();
        if(cookies != null)
            for(Cookie cookie : cookies){
                if(cookie.getName().equalsIgnoreCase("sign"))
                    sign = cookie.getValue();
            }
        return sign;
    }

    /**
     * @deprecation 获取用户id
     * @return Integer
     * @author 雷楚桥
     * @date 2018-09-02 15:27
     */
    public Integer thisUserId(){
        return checkSign(getSign());
    }

    /**
     * @deprecation 检查用户对于该uri是否有权限
     * @param userId String 用户id
     * @param uri String 访问的uri
     * @return boolean
     * @author 雷楚桥
     * @date 2018-09-01 15:17
     */
    @SuppressWarnings("unchecked")
    public boolean checkAuth(String userId, String uri) {
        List<String> authList = (List<String>)session.getAttribute("authList");
        if(authList == null){
            authList = new ArrayList<>();
            String authStr = authS.findValueByUserId(userId).trim();
            if(authStr != null){
                authList = Arrays.asList(authStr.split(","));
                session.setAttribute("authList",authList);
            }
        }
        System.out.println(authList.get(1));
        return authList.contains(uri);
    }

    /**
     * @deprecation 检查签名是否失效,如果没有失效，则返回登陆userId,否则为null
     * @param sign String 签名
     * @return userId | null
     * @author 雷楚桥
     * @date 2018-09-02 11:07
     */
    public Integer checkSign(String sign) {
        if(sign != null && redis.hasKey(sign))
            return Integer.parseInt(redis.opsForValue().get(sign));
        return null;
    }

    /**
     * @deprecation 得到当前这个部门的id
     * @return Integer
     * @author 雷楚桥
     * @date 2018-09-02 17:50
     */
    public Integer thisBranchId() {
        return userS.findBranchIdByUserId(checkSign(getSign()));
    }
}
